package com.market.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "promotion")
public class Promotion extends BaseObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3949614486561051511L;

	@Override
	public boolean equalsId(Object o) {
		if ((o instanceof Promotion) && ((Promotion) o).getId().equals(this.id)) {
			return true;
		}
		return false;
	}

	public String getTitleLabel() {
		return super.getLabel("Title", super.getLocale());
	}

	public String getCommentsLabel() {
		return super.getLabel("Comments", super.getLocale());
	}
	
	public void validCode(){
		if(this.code.equalsIgnoreCase(this.userInputCode)){
			this.isCodeValid=Boolean.TRUE;
		}else{
			this.isCodeValid=Boolean.FALSE;
		}
	}

	@Transient
	private Boolean isCodeValid = Boolean.FALSE;
	@Transient
	private Boolean isMinAmountValid = Boolean.FALSE;
	@Transient
	private String userInputCode; // Use this for user input, then check against
								// code, correct then set isCodeValid = true;
	
	

	public Boolean getIsCodeValid() {
		return isCodeValid;
	}

	public void setIsCodeValid(Boolean isCodeValid) {
		this.isCodeValid = isCodeValid;
	}

	public Boolean getIsMinAmountValid() {
		return isMinAmountValid;
	}

	public void setIsMinAmountValid(Boolean isMinAmountValid) {
		this.isMinAmountValid = isMinAmountValid;
	}

	public String getUserInputCode() {
		return userInputCode;
	}

	public void setUserInputCode(String userInputCode) {
		this.userInputCode = userInputCode;
	}
	

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = 0L;
	@Column(length = 50,nullable = false,unique=true)
	private String number;
	@Column(length = 300)
	private String title;
	@Column(length = 300)
	private String chineseTitle;
	@Column(length = 300)
	private String frenchTitle;
	@Column
	@Temporal(TemporalType.DATE)
	private Date startDate;
	@Column
	@Temporal(TemporalType.DATE)
	private Date endDate;
	@Column(columnDefinition = "int Default 0")
	private Integer rewardRate = 0;
	@Column(columnDefinition = "int Default 0")
	private Integer minAmount = 0;
	@Column(length = 100)
	private String marketingMedia;
	@Column(length = 100)
	private String code;
	@Column(length = 1000)
	private String comments;
	@Column(length = 1000)
	private String chineseComments;
	@Column(length = 1000)
	private String frenchComments;
	@ManyToMany( fetch = FetchType.LAZY)
	private List<Location> locations = new ArrayList<Location>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getChineseTitle() {
		return chineseTitle;
	}

	public void setChineseTitle(String chineseTitle) {
		this.chineseTitle = chineseTitle;
	}

	public String getFrenchTitle() {
		return frenchTitle;
	}

	public void setFrenchTitle(String frenchTitle) {
		this.frenchTitle = frenchTitle;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Integer getRewardRate() {
		return rewardRate;
	}

	public void setRewardRate(Integer rewardRate) {
		this.rewardRate = rewardRate;
	}

	public Integer getMinAmount() {
		return minAmount;
	}

	public void setMinAmount(Integer minAmount) {
		this.minAmount = minAmount;
	}

	public String getMarketingMedia() {
		return marketingMedia;
	}

	public void setMarketingMedia(String marketingMedia) {
		this.marketingMedia = marketingMedia;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public String getChineseComments() {
		return chineseComments;
	}

	public void setChineseComments(String chineseComments) {
		this.chineseComments = chineseComments;
	}

	public String getFrenchComments() {
		return frenchComments;
	}

	public void setFrenchComments(String frenchComments) {
		this.frenchComments = frenchComments;
	}

	public List<Location> getLocations() {
		return locations;
	}

	public void setLocations(List<Location> locations) {
		this.locations = locations;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result
				+ ((comments == null) ? 0 : comments.hashCode());
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result
				+ ((marketingMedia == null) ? 0 : marketingMedia.hashCode());
		result = prime * result
				+ ((minAmount == null) ? 0 : minAmount.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Promotion other = (Promotion) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (comments == null) {
			if (other.comments != null)
				return false;
		} else if (!comments.equals(other.comments))
			return false;
		if (endDate == null) {
			if (other.endDate != null)
				return false;
		} else if (!endDate.equals(other.endDate))
			return false;
		if (marketingMedia == null) {
			if (other.marketingMedia != null)
				return false;
		} else if (!marketingMedia.equals(other.marketingMedia))
			return false;
		if (minAmount == null) {
			if (other.minAmount != null)
				return false;
		} else if (!minAmount.equals(other.minAmount))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Promotion [id=" + id + ", title=" + title + ", startDate="
				+ startDate + ", endDate=" + endDate + ", minAmount="
				+ minAmount + ", marketingMedia=" + marketingMedia + ", code="
				+ code + ", comments=" + comments + "]";
	}

}
